/**
 * 字符串转换为数值
 */

#include <cstdlib>
#include <iostream>

void atof_test() {
    // 函数会舍弃任何空白符（由 std::isspace() 确定），直至找到首个非空白符。
    // 然后它会取用尽可能多的字符，以构成合法地浮点数表示，并将它们转换成浮点值
    const char *str1 = "0.0000000123";
    const char *str2 = "0.012abc";
    const char *str3 = "15e16";
    const char *str4 = "-0x1afp-2";
    const char *str5 = "inF";
    const char *str6 = "-";
    const char *str7 = "/";
    const char *str8 = "inF";
    const char *str9 = "invalid";
    std::cout << str1 << " to " << std::atof(str1) << std::endl;
    std::cout << str2 << " to " << std::atof(str2) << '\n';
    std::cout << str3 << " to " << std::atof(str3) << '\n';
    std::cout << str4 << " to " << std::atof(str4) << '\n';
    std::cout << str5 << " to " << std::atof(str5) << '\n';
    std::cout << str6 << " to " << std::atof(str6) << '\n';
    std::cout << str7 << " to " << std::atof(str7) << '\n';
    std::cout << str8 << " to " << std::atof(str7) << '\n';
    std::cout << str9 << " to " << std::atof(str9) << '\n';

}

int atoi_test() {
    // 舍弃任何空白符，直至找到首个非空白符，然后接收尽可能多的字符以组成合法的整数表示，并转换之为整数值。
    // 合法的整数值含：正或负号, 数位
    const char *str1 = "42";
    const char *str2 = "3.14159";
    const char *str3 = "31337 with words";
    const char *str4 = "words and 2";
    const char *str5 = " -2";
    std::cout << str1 << " to " << std::atoi(str1) << '\n';
    std::cout << str2 << " to " << std::atoi(str2) << '\n';
    std::cout << str3 << " to " << std::atoi(str3) << '\n';
    std::cout << str4 << " to " << std::atoi(str4) << '\n';
    std::cout << str5 << " to " << std::atoi(str5) << '\n';
    return 0;
}

int main() {
    atof_test();
    std::cout << "=====" << '\n';
    atoi_test();
}